var isInitData = true;
var isInit = true;
var selectLeftData = '',
    selectRightData = '';

var vueObj = new Vue({
    el: "#vue",
    data: {
        searchInfo : searchInfo
    }
});


function doRefresh() {
    var title = $('#title').val();
    var status = $('#status').val();
    var deptCode = $('#deptCode').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'operative/banner/pageFindBannerData?t=' + new Date().getTime(),
        query: {
            title : title,
            status : status,
            deptCode : deptCode,
            bannerType : selectLeftData ? selectLeftData.dictCode : '',
        },
    });
}

function getParams(params) {
    var title = $('#title').val();
    var status = $('#status').val();
    var deptCode = $('#deptCode').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    var bannerType = selectLeftData ? selectLeftData.dictCode : '';
    if (isInit) {
        isInit = false;
        pageSize = searchInfo ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo ? searchInfo.pageNo : params.pageNumber;
        bannerType = searchInfo.bannerType;
    }
    return {
        title : title,
        status : status,
        deptCode : deptCode,
        bannerType : bannerType,
        pageSize : pageSize,
        pageNo : pageNo
    };
}


function openAddBannerPage(){
    if(!selectLeftData || selectLeftData == ''){
        toastr.error('请选择banner位名称');
        return;
    }
    var title = $("#title").val();
    var deptCode = $("#deptCode").val();
    var status = $("#status").val();
    window.location.href = basePath + 'operative/banner/editBannerPage?bannerType='+selectLeftData.dictCode+'&title='+title+'&deptCode='+deptCode
        +'&status='+status+'&t=' + new Date().getTime();
}

function openEditBannerPage() {
    if (!selectRightData || selectRightData == '') {
        toastr.error('请先选择一条记录后再进行编辑');
        return;
    }
    var title = $("#title").val();
    var deptCode = $("#deptCode").val();
    var status = $("#status").val();
    //var bannerType = selectRightData.bannerType;
    var bannerType = selectLeftData.dictCode;
    window.location.href = basePath + 'operative/banner/editBannerPage?id='+selectRightData.id+'&bannerType='+bannerType+'&title='
        +title+'&deptCode='+deptCode+'&status='+status+'&t=' + new Date().getTime();
}

function delBanner(){
    if (!selectRightData || selectRightData == '') {
        toastr.error('请先选择一条记录后再进行删除');
        return;
    }
    var str = '确认删除吗？';
    swalToastr('是否删除', str, 'warning', doDelBanner, '确认删除');
}

function doDelBanner(){
    $.ajax({
        type: 'post',
        url: basePath + 'operative/banner/delBanner?id='+selectRightData.id+'&t=' + new Date().getTime(),
        dataType: 'json',
        success : function (r) {
            if(r.result == true){
                toastr.success('删除成功');
                //window.location.href = basePath + 'operative/banner/bannerPage?t=' + new Date().getTime();
                doRefresh();
            }else{
                toastr.error("删除失败");
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

//提交发布
function pubishBanner() {
    if(selectRightData == ''){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if(selectRightData.status == 'published'){
        toastr.error("已发布数据无需提交发布");
        return;
    }
    var str = '确认提交发布吗？';
    swalToastr('提交发布', str, 'warning', doCommitBanner, '确认提交');
}


function doCommitBanner(){
    $.ajax({
        type: 'post',
        url: basePath + 'operative/banner/updateStatus',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            id: selectRightData.id,
        }),
        success: function (data) {
            if (data.result) {
                toastr.success(data.message);
               // window.location.href = basePath + 'operative/banner/bannerPage?t=' + new Date().getTime();
                doRefresh();
            } else {
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}


//撤销发布
function unPublishBanner() {
    if(selectRightData == ''){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if(selectRightData.status == 'unpublished'){
        toastr.error("待提交数据无需撤销发布");
        return;
    }
    if(selectRightData.status == 'revoked'){
        toastr.error("已撤销数据无需撤销发布");
        return;
    }
    var str = '确认撤销发布吗？';
    swalToastr('撤销发布', str, 'warning', doRevokeBanner, '确认撤销');

}


function doRevokeBanner(){
    $.ajax({
        type: 'post',
        url: basePath + 'operative/banner/revokeBanner',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            id: selectRightData.id,
        }),
        success: function (data) {
            if (data.result) {
                toastr.success(data.message);
                //window.location.href = basePath + 'operative/banner/bannerPage?t=' + new Date().getTime();
                doRefresh();
            } else {
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}


//左边选中事件
$("#leftTable").on("click-row.bs.table",function(e,row,ele){
    $("#leftTable .success").removeClass("success");
    $(ele).addClass("success");
    selectLeftData = row;
    doRefresh();
});

//右边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectRightData = row;
});

function initCheck(value, row, index) {
    if (isInitData && row.dictCode == searchInfo.bannerType) {
        setTimeout(function(){
            $("#leftTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },200);
        isInitData = false;
        selectLeftData = row;
    }
    return value;
}


function formatStatus(value, row, index) {
    if (statusMap) {
        return statusMap[value];
    }
    return value;
}

function formatType(value, row, index){
    if(contentType){
        return contentType[value];
    }
    return value;
}